package agh.gs.semdrools;

import org.drools.KnowledgeBase;
import org.drools.agent.KnowledgeAgent;
import org.drools.runtime.StatefulKnowledgeSession;


public class AgentSessionProvider implements SessionProvider {
	
	public AgentSessionProvider( KnowledgeAgent agent ) {
		
		this.agent = agent;
		
	}
	
	
	public StatefulKnowledgeSession provide() {
		
		if ( ( session == null ) || ( base == null )
		     || ( base != agent.getKnowledgeBase() ) ) {
			
			if ( session != null ) {
				session.dispose();
			}
			
			base = agent.getKnowledgeBase();
			session = base.newStatefulKnowledgeSession();
			session.fireAllRules();
			
		}
		
		return session;
		
	}
	
	
	private KnowledgeAgent agent = null;
	private KnowledgeBase base = null;
	private StatefulKnowledgeSession session = null;

}
